package net.starwifi.duobao.plugin.wo3.tool;


import cn.com.hongpay.client.gen.ReqMsgGenerate;
import cn.com.hongpay.client.vo.pay.SinglePayNotifyResVo;
import net.starwifi.duobao.SettingUtils;
import net.starwifi.duobao.entity.DBProduct;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;

import java.util.Date;


/**
 * Created by Administrator on 2015/7/17.
 */

public class PaymentNotifyRequest {


    private String partnerId;

    private String signedMsg;

    private String tradeNo;

    private String partnerTradeNo; //合作方流水号

    private String resultCode; //00成功，99失败

    private String detailRemark; //明细备注

    private String amount; //金额

    private String acctReservedField1;

    private String acctReservedField2;

    private String acctReservedField3;

    @Override
    public String toString() {
        return "PaymentNotifyRequest{" +
                "partnerId='" + partnerId + '\'' +
                ", signedMsg='" + signedMsg + '\'' +
                ", tradeNo='" + tradeNo + '\'' +
                ", partnerTradeNo='" + partnerTradeNo + '\'' +
                ", resultCode='" + resultCode + '\'' +
                ", detailRemark='" + detailRemark + '\'' +
                ", amount='" + amount + '\'' +
                ", acctReservedField1='" + acctReservedField1 + '\'' +
                ", acctReservedField2='" + acctReservedField2 + '\'' +
                ", acctReservedField3='" + acctReservedField3 + '\'' +
                '}';
    }

    public static PaymentNotifyRequest getNotify(String xml){
        PaymentNotifyRequest request=new PaymentNotifyRequest();
        try {
            xml = xml.replaceAll("\n", "");
            xml = xml.replaceAll("\t", "");
            Document doc= DocumentHelper.parseText(xml);
            //合作伙伴ID
            org.dom4j.Element elementPartnerID = (org.dom4j.Element) doc.selectSingleNode("/AIPAYTRADE/INFO/PARTNER_ID");
            request.setPartnerId(elementPartnerID.getStringValue());

            //签名
            org.dom4j.Element elementSign = (org.dom4j.Element) doc.selectSingleNode("/AIPAYTRADE/INFO/SIGNED_MSG");
            request.setSignedMsg(elementSign.getStringValue());

            //订单号
            org.dom4j.Element elementTradeNo = (org.dom4j.Element) doc.selectSingleNode("/AIPAYTRADE/BODY/TRADE_DETAILS/TRANS_DETAIL/TRADE_NO");
            request.setTradeNo(elementTradeNo.getStringValue());

            //我方订单号
            org.dom4j.Element elementPartnerTradeNo = (org.dom4j.Element) doc.selectSingleNode("/AIPAYTRADE/BODY/TRADE_DETAILS/TRANS_DETAIL/PARTNER_TRADE_NO");
            request.setPartnerTradeNo(elementPartnerTradeNo.getStringValue());

            //支付状态
            org.dom4j.Element elementResultCode = (org.dom4j.Element) doc.selectSingleNode("/AIPAYTRADE/BODY/TRADE_DETAILS/TRANS_DETAIL/RESULT_CODE");
            request.setResultCode(elementResultCode.getStringValue());

            //支付状态备注
            org.dom4j.Element elementDetailRemark = (org.dom4j.Element) doc.selectSingleNode("/AIPAYTRADE/BODY/TRADE_DETAILS/TRANS_DETAIL/DETAIL_REMARK");
            request.setDetailRemark(elementDetailRemark.getStringValue());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return request;

    }

    public String getResponse(String code,Integer flag){
        SinglePayNotifyResVo notifyResVo=new SinglePayNotifyResVo();
        String pfxFile=null;
        String pfxPwd=null;
        String privateKeyPwd=null;
        String vPartnerId=null;
        String partnerVerifyCode=null;
        String keyPath= SettingUtils.get("keyPath");
        pfxFile = keyPath+"/starwifi_private.pfx"; // 合作方的私钥文件路径
        pfxPwd = "starwifi_7982!"; // 私钥库密码
        privateKeyPwd = "starwifi@1532%";// 私钥密码
        vPartnerId="100004";
        partnerVerifyCode="343D35167272373596FBA8291C6AB3B8";
        if(flag!=null&&flag.equals(DBProduct.FLAG_YX)){
            vPartnerId="100012";
            partnerVerifyCode="2B989953E6B4F3F83A8F870796844272";
        }
        notifyResVo.setPartnerVerifyCode(partnerVerifyCode);
        notifyResVo.setPartnerId(vPartnerId);
        notifyResVo.setPartnerTradeNo(getPartnerTradeNo());
        notifyResVo.setRequestTime(new Date());
        notifyResVo.setTradeNo(getTradeNo());
        notifyResVo.setResultCode(code);

        ReqMsgGenerate reqMsgGenerate = new ReqMsgGenerate(pfxFile,  pfxPwd,privateKeyPwd);
        reqMsgGenerate.setCharset("UTF-8");
        return reqMsgGenerate.generatePayNotifyResMsg(notifyResVo);
    }


    public String getPartnerId() {
        return partnerId;
    }

    public void setPartnerId(String partnerId) {
        this.partnerId = partnerId;
    }

    public String getSignedMsg() {
        return signedMsg;
    }

    public void setSignedMsg(String signedMsg) {
        this.signedMsg = signedMsg;
    }

    public String getTradeNo() {
        return tradeNo;
    }

    public void setTradeNo(String tradeNo) {
        this.tradeNo = tradeNo;
    }

    public String getPartnerTradeNo() {
        return partnerTradeNo;
    }

    public void setPartnerTradeNo(String partnerTradeNo) {
        this.partnerTradeNo = partnerTradeNo;
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public String getDetailRemark() {
        return detailRemark;
    }

    public void setDetailRemark(String detailRemark) {
        this.detailRemark = detailRemark;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getAcctReservedField1() {
        return acctReservedField1;
    }

    public void setAcctReservedField1(String acctReservedField1) {
        this.acctReservedField1 = acctReservedField1;
    }

    public String getAcctReservedField2() {
        return acctReservedField2;
    }

    public void setAcctReservedField2(String acctReservedField2) {
        this.acctReservedField2 = acctReservedField2;
    }

    public String getAcctReservedField3() {
        return acctReservedField3;
    }

    public void setAcctReservedField3(String acctReservedField3) {
        this.acctReservedField3 = acctReservedField3;
    }
}
